package com.tinklabs.demo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.tinklabs.demo.constants.BundleConstants;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Jennifer on 2018/4/14.
 */
public abstract class BaseActivity extends AppCompatActivity {
    private Unbinder unbinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //设置layout
        setContentView(getLayout());

        unbinder = ButterKnife.bind(this);

        //初始化头部
        initTitle();

        //初始化数据
        initData();

        //初始化内容
        initView(savedInstanceState);
    }

    protected abstract int getLayout();

    protected void initTitle() {
    }

    protected abstract void initData();

    protected abstract void initView(Bundle savedInstanceState);

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        reset();
    }

    protected void reset() {
    }

    public void startAction(Class<?> cl, Bundle bundle) {
        Intent intent = new Intent(this, cl);
        if (bundle != null) {
            intent.putExtra(BundleConstants.BUNDLE_KEY, bundle);
        }
        startActivity(intent);
    }

    public Bundle getBundle() {
        Bundle bundle = getIntent().getBundleExtra(BundleConstants.BUNDLE_KEY);
        if (bundle == null) {
            bundle = new Bundle();
        }
        return bundle;
    }
}
